// 轮播1

let sliderBox = document.querySelector(".sliderBox");
      let sliderLists = sliderBox.children[0].children;
      let aBtns = document.querySelector(".btns").children;
      let aCircles = document.querySelector(".circles").children;
      let i = 0; //对应区块的索引

      function move() {
        i++;

        //123123
        if (i == sliderLists.length) {
          //临界值处理
          i = 0;
        }
        //321321
        if (i == -1) {
          i = sliderLists.length - 1;
        }
        
        //小圆点
        for (let j = 0; j < aCircles.length; j++) {
          aCircles[j].className = "";
        }
        aCircles[i].className = "hover";
        /* sliderLists[i].style.opacity = 1; */
        //先清再加
         for (let j = 0; j < sliderLists.length; j++) {
          startMove(sliderLists[j], { opacity: 0 });
        }
        startMove(sliderLists[i], { opacity: 100 });

        /* for (let j = 0; j < sliderLists.length; j++) {
          sliderLists[j].style.opacity = 0;
        }
        sliderLists[i].style.opacity = 1; */
      }
      let timer = setInterval(() => {
        move();
      }, 3000);

      //右箭头
      aBtns[1].onclick = () => {
        move();
      };
      //左箭头
      aBtns[0].onclick = () => {
        i -= 2; //move里有一个i++
        move();
      };

      //小圆点移入
      for (let j = 0; j < aCircles.length; j++) {
        aCircles[j].onmouseover = () => {
          i = j - 1;
          move();
        };
      }

      //移入移出轮播区域，开关定时器
      sliderBox.onmouseover = () => {
        clearInterval(timer);
      };
      sliderBox.onmouseout = () => {
        timer = setInterval(() => {
          move();
        }, 3000);
      };

    //   独家
let topLis = document.querySelectorAll('.sortshow-topli');
let showBoxs = document.querySelectorAll('.sortshow-box');
for(let i = 0;i < topLis.length;i ++){
    topLis[i].onmouseover = function (){
        for(let j = 0;j < topLis.length;j ++){
           topLis[j].classList.remove('on');
           showBoxs[j].classList.remove('block');
        }
        topLis[i].className = 'sortshow-topli on';
        showBoxs[i].className = 'sortshow-box block';
    }
}

// 图书畅销榜
let conlis = document.querySelectorAll('.con-li');
let content = document.querySelector('.content2');
let conul = content.children;
for(let i = 0;i < conlis.length;i ++){
   conlis[i].onmouseenter = () =>{
    for(let j = 0; j < conlis.length;j ++){
        conlis[j].classList.remove('more-border');
        conul[j].style.display = 'none';
    }
    this.className = 'con-li more-border';
    conul[i].style.display = 'block';
   }

   /* let con_lis = conul[i].children;
    console.log(con_lis);
    for(let k = 0;k < con_lis.length;k ++){
        
        con_lis[k].onmouseenter = () =>{
            let con_ = con_lis[k].children[1];
        // console.log(con_);
            for(let t = 0;t < con_lis.length;t ++){

                con_lis.children[1].children[0].classList.remove('none');
                con_lis.children[1].children[1].classList.remove('block');
            }
            con_.children[0].className = 'none';
            con_.children[1].className = 'block';
        }
    } */
    
}


// 热门作者

let res = document.querySelectorAll('.rem');
for(let i = 0;i < res.length;i ++){
  res[i].onmouseover = function (){
      for(let j = 0;j < res.length;j ++){
         res[j].classList.remove('re-bor');
         
      }
      res[i].className = 'rem re-bor';
      
  }
}

// 导航栏
let icon_1 = document.querySelector('.icon1');
console.log(icon_1);
let icon_2 = document.querySelector('.icon2');
let select = document.getElementById('select');
let sentbox = document.querySelector('.select-box');
let box = document.querySelector('.box')
box.onmouseover = function (){
  icon_1.style.display = 'none';
  icon_2.style.display = 'block';
  sentbox.style.opacity = '1';
  
  let sentlis = sentbox.children;
// console.log(sentlis);
let sentspan = document.querySelector('.inner-se');
for(let i = 0;i < sentlis.length;i ++){
  // console.log(sentlis[i]);
  sentlis[i].onclick = function (){
    sentspan.innerHTML= ' ';
    sentspan.innerHTML= this.children[0].innerText;

  }
}
}
box.onmouseout = function (){
  icon_1.style.display = 'block';
  icon_2.style.display = 'none';
  sentbox.style.opacity = '0';
}


//返回顶部
var oBtn = document.getElementById('top_btn');
// let timer = '';
var beSys = false;
oBtn.style.display = 'none'
window.onscroll = function() {
        // 判断：是定时器触发的还是人为触发
        oBtn.style.display = 'block';
        if (beSys == false) {
            clearInterval(timer);
        }
        beSys = false;
    }
    //onscroll 只要滚动条改变都会触发
oBtn.onclick = function() {
    clearInterval(timer);
    timer = setInterval(function() {
        beSys = true;
        // 1：获取当前值
        var oScrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        // 2：算速度
        var iSpeed = -oScrollTop / 60;
        iSpeed = Math.floor(iSpeed); //向下取整 
        //3: 判断
        if (oScrollTop == 0) {
            oBtn.style.display = 'none';
            clearInterval(timer);
        } else {
            document.documentElement.scrollTop = document.body.scrollTop = oScrollTop + iSpeed;
        }

    }, 1);

}
 //摸图片字hover
    var oVipimg = document.querySelectorAll('.vipimg')
    var oVipbookname = document.querySelectorAll('.vipbookname')
    for (let i = 0; i < oVipimg.length; i++) {
        oVipimg[i].index = i;
        oVipimg[i].onmouseover = function() {
            oVipbookname[this.index].style.color = '#ee8429';
            oVipbookname[this.index].style.textDecoration = 'underline';
        }
    }
    for (var j = 0; j < oVipimg.length; j++) {
        oVipimg[j].index = j;
        oVipimg[j].onmouseout = function() {
            oVipbookname[this.index].style.color = '';
            oVipbookname[this.index].style.textDecoration='none';
        }
    }

    /* var oRight3 = document.querySelector('.right3')
    var oRight2 = document.querySelector('.right2')
    var oRight4 = document.querySelector('.right4')
    var oRight5 = document.querySelector('.right5')
    var oRight6 = document.querySelector('.right6')
    var oRight7 = document.querySelector('.right7')
    var oRight8 = document.querySelector('.right8')
    oRight3[0].onmouseover = function() {
        oRight2[0].style.display = 'block';
        oRight3[0].style.display = 'none';
        oRight4[0].style.display = 'block';
    }
    oRight2[0].onmouseout = function() {
        oRight3[0].style.display = 'block';
        oRight2[0].style.display = 'none';
        oRight4[0].style.display = 'none';
    }
    oRight5[0].onmouseover = function() {
        oRight6[0].style.display = 'block';
        oRight5[0].style.display = 'none';
    }
    oRight6[0].onmouseout = function() {
        oRight5[0].style.display = 'block';
        oRight6[0].style.display = 'none';
    } */

    function setCookie(key, value, expires) {
      if (expires) {
          let oDate = new Date();
          oDate.setDate(oDate.getDate() + expires);
          document.cookie = `${key}=${value};expires=${oDate}`;
        } else {
          document.cookie = `${key}=${value}`;
        }
    }
  
  
    function getCookie(key) {
      let str = document.cookie;
      let arr = str.split(";");
      let value;
      /* str.forEach(item => {
        // console.log(item);
        let arr = item.split('=');
        if(arr[0] == "key"){
          value = arr[1]
        }
      });
      return value; */
      for (let i = 0; i < arr.length; i++) {
        let brr = arr[i].split("=");
        if (brr[0] === key) {
          return brr[1];
        }
      }
    }
  
    
    function removeCookie(key) {
      setCookie(key, 1, -1);
    }

    let sortshowul = document.querySelector('.sortshow-box_ul')
    
    let str = "";
$.get("http://jx.xuzhixiang.top/ap/api/allproductlist.php", {
        pagesize: 10,
        pagenum: 0,
        uid: 139869,
      }).then((resData) => {
        console.log(resData.data);
            resData.data.forEach((item) => {
          str += `
          <li class = "li" data-id="${item.pid}"><div class="showcard">
          <img class="card-img-top" src="${item.pimg}"/>
          <div class="card-body">
           <span class="clon">${item.pdesc}</span>
            <p class="card-text">当当独家特供</p>
            <span>${item.pprice}<del>￥59.00</del></span>
          </div>
        </div>
      </li> 
        `;
           

        });
        let pid = null;//存每次取到的id
        sortshowul.innerHTML = str;
        let List = document.querySelectorAll(".li")
        console.log(List);
        List.forEach(item=>{
          item.onclick = ()=>{
            pid = item.getAttribute("data-id")//去自定义属性
            console.log(pid);
            setCookie("pid",pid)

            location.href = 'list.html';
          }
        })
      });

      



